/**
 * @format
 */

import {AppRegistry, BackHandler} from 'react-native';
import LoginLeaf from './LoginLeaf';
import WaitingLeaf from './WaitingLeaf';
import React, {Component} from 'react';
import {name as appName} from './app.json';
export default class NaviModule extends Component {
  constructor(props) {
    super(props);
    this.state = {
      currentScene: 'Login',
      phoneNumber: '',
      userPW: '',
    };
    this.handleBackSignal = this.handleBackSignal.bind(this);
    this.onLoginPressed = this.onLoginPressed.bind(this);
  }
  onLoginPressed(aNumber, aPW) {
    this.setState({
      currentScene: 'Waiting',
      phoneNumber: aNumber,
      userPW: aPW,
    });
  }
  render() {
    if (this.state.currentScene === 'Login') {
      return <LoginLeaf onLoginPressed={this.onLoginPressed} />;
    } else {
      return (
        <WaitingLeaf
          phoneNumber={this.state.phoneNumber}
          onGobackPressed={this.handleBackSignal}
          userPW={this.state.userPW}
        />
      );
    }
  }
  handleBackSignal() {
    if (this.state.currentScene === 'Waiting') {
      this.setState({currentScene: 'Login'});
      return true;
    }
    return false;
  }
  componentDidMount() {
    BackHandler.addEventListener('handwareBackPress', this.handleBackSignal);
  }
  componentWillUnmount() {
    BackHandler.removeEventListener('handwareBackPress', this.handleBackSignal);
  }
}
AppRegistry.registerComponent(appName, () => NaviModule);
